using UnityEngine;
using System;
using System.Reflection;
using UnityEngine.EventSystems;

namespace Super
{
	public static class InitUtil
	{
		/// <summary>
		/// ��ʼ����Ϸ��Manger
		/// Manager�߼�д������Ins��Get������ֻ�����Ins������ɳ�ʼ��
		/// �������غ󣬼�������Awake��OnEnable����ִ�к���ú���
		/// �����ǿ�����������Ϸ���������ص�һ������ʱ�����ض��ķ���
		/// </summary>
		[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
		private static void OnGameLoadInit()
		{
			/*******************************���ϵͳ��ʼ��*****************************/

			LoadRootGOByInitState("EventSystem", FWConfig.eventSystem, "EventSystem");
			LoadRootGOByInitState("UIRoot", FWConfig.UIRoot, "UIRoot");
			LoadRootGOByInitState("UICamera", FWConfig.UICamera, "UICamera");
			LoadRootGOByInitState("Main Camera", FWConfig.mainCamera, "Main Camera");
			LoadRootGOByInitState("Virtual Camera", FWConfig.virtualCamera, "VirtualCamera");
			LoadRootGOByInitState("WorldUIRoot", FWConfig.worldUIRoot, "WorldUIRoot");
			LoadRootGO("ManagerBox", FWConfig.managerBox);

			GameUtil.MainCameraStackAdd(DataHelperUtil.GetUICamera());
			InputManager.Ins.Init();
			TimerManager.Ins.Init();
			LangManager.Ins.Init();
			VirtualCM2DManager.Ins.Init();

			/*******************************����Manager��ʼ��*****************************/
			InitManager();
		}

		/// <summary>
		/// ��ѡ������ã���ͼ���غ�ȡ����Ϸ���ڣ���ɫ����͸����
		/// </summary>
		[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
		private static void OnBeforeSplashScreen()
		{
#if UNITY_STANDALONE_WIN
			if (FWSetManager.GetSwitchState("Borderless"))
			{
				WindowUtil.RemoveBorder();
			}

			if (FWSetManager.GetSwitchState("BlackTransparency"))
			{
				WindowUtil.SetColorAlpha(new Color(0, 0, 0, 0));
			}
#endif
		}

		public static void LoadRootGO(string name, string AName)
		{
			GameObject go = DataHelperUtil.FindDontDestroyObject(name);
			bool isExist = go != null;
			if (isExist) { return; }

			GameUtil.GetOrCreateSceneRoot(name, AName);
		}

		public static void LoadRootGOByInitState(string name, string AName, string initKey)
		{
			bool state = FWSetManager.GetInitState(initKey);
			if (state)
			{
				LoadRootGO(name, AName);
			}
		}

		/// <summary>
		/// ��ʼ��Manager���ұ����ǵ���
		/// </summary>
		public static void InitManager()
		{
			Type[] types = typeof(IInit).Assembly.GetTypes();
			MethodInfo methodInfo;
			object instance;
			foreach (var item in types)
			{
				if (!item.IsAbstract && !item.IsInterface && !item.IsGenericType && typeof(IInit).IsAssignableFrom(item))
				{
					instance = Activator.CreateInstance(item);
					methodInfo = instance.GetType().GetMethod("OnInit");
					methodInfo.Invoke(instance, null);
				}
			}
		}
	}

}
